﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices; 

namespace HOOMT
{
    public partial class AttributesDialog : Form
    {
        //“临时”属性列表，根据添加属性对话框实时更新属性对话框的显示，（显示更新)
        //属性对话框返回 DialogResult.OK 时，用临时属性列表更新对象的属性列表（数据更新）

        private List<Attribute> temporaryAttributeList = new List<Attribute>();
        private int myLine, offSet;//记录选中行号，选中行偏移(行起始位置)
        private bool hasSelectedItem = false;
        //####################################################################################
        public AttributesDialog()
        {
            InitializeComponent();
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
            this.MaximizeBox = false;
        }
        
        //************************************************************************************
        public RichTextBox AttributeShowBox
        {
            get{return attributesShowBox;}
            set{attributesShowBox = value;}
        }
        public List<Attribute> TemporaryAttributeList
        {
            get{return temporaryAttributeList;}
            set{temporaryAttributeList = value;}
        }
        private void AttributesDialog_Load(object sender, EventArgs e)
        {

        }
        //************************************************************************************
        //添加属性项并更新属性对话框的显示
        private void addButton_Click(object sender, EventArgs e)
        {
            AttributeAddDialog addDialog = new AttributeAddDialog();
            if (addDialog.ShowDialog() == DialogResult.OK)
            {
                if (!String.IsNullOrWhiteSpace(addDialog.AttributeType) && !String.IsNullOrWhiteSpace(addDialog.AttributeName))
                {
                    //要求去掉前后空格
                    Attribute attribute = new Attribute(addDialog.AccessModifier.Trim(), addDialog.AttributeType.Trim(), 
                        addDialog.AttributeName.Trim(), addDialog.InitialValue.Trim(), addDialog.Description.Trim());
                    this.AttributeShowBox.Text += attribute.ToString();
                    this.AttributeShowBox.Text += "\n";
                    this.TemporaryAttributeList.Add(attribute);
                    //zf
                    //    XmlHelper xmlTool = XmlHelper.GetInstance();
                    //     xmlTool.AddAttribute(attribute);
                }
                else
                {
                    if (String.IsNullOrWhiteSpace(addDialog.AttributeType))
                        MessageBox.Show("Invalid Inputs，AttributeType is null!");
                    else
                        MessageBox.Show("Invalid Inputs，AttributeName is null!");
                }
            }
            hasSelectedItem = false;//复位
        }
        //************************************************************************************
        private void modifyButton_Click(object sender, EventArgs e)
        {
            if (hasSelectedItem)//在有选中条件下操作
            {
                String str = attributesShowBox.Lines[myLine];
                AttributeAddDialog modifyForm = new AttributeAddDialog();
                modifyForm.Text = "AttributeModifyDialog";
                //设置方法对话框的初始显示值
                String[] arrstr;
                arrstr = str.Split();
                modifyForm.AccessModifier = arrstr[0];
                modifyForm.AttributeType = arrstr[2];//因为中间有两个空格
                modifyForm.AttributeName = arrstr[4];
                modifyForm.InitialValue = arrstr[6].Substring(0, arrstr[6].Length - 1);//去掉最后的分号
                modifyForm.Description = arrstr[8];
                /*String nameAndParameter = arrstr[4];
                for (int i = 0; i < nameAndParameter.Length; i++)
                {
                    if (nameAndParameter[i].Equals('('))
                    {
                        modifyForm.MethodName = nameAndParameter.Substring(0, i);
                        modifyForm.ParameterList = nameAndParameter.Substring(i + 1, nameAndParameter.Length - i - 3);
                        break;
                    }
                }*/
                //更新修改后的显示和temporaryMethodList列表
                if (modifyForm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    if (!String.IsNullOrWhiteSpace(modifyForm.AttributeType) && !String.IsNullOrWhiteSpace(modifyForm.AttributeName))
                    {
                        Attribute attr = new Attribute(modifyForm.AccessModifier.Trim(), modifyForm.AttributeType.Trim(), 
                            modifyForm.AttributeName.Trim(), modifyForm.InitialValue.Trim(), modifyForm.Description.Trim());
                        //更新显示
                        StringBuilder str2 = new StringBuilder();
                        str2.Append(attributesShowBox.Text);
                        str2.Replace(str, attr.ToString());
                        attributesShowBox.Text = str2.ToString();
                        //更新临时列表
                        temporaryAttributeList[myLine] = attr;
                    }
                    //Invalidate();
                }
                hasSelectedItem = false;//复位
            }
        }
        //************************************************************************************
        private void deleteButton_Click(object sender, EventArgs e)
        {
            if (hasSelectedItem)//在有选中条件下操作
            {
                StringBuilder str1 = new StringBuilder();
                str1.Append(attributesShowBox.Text);
                str1.Remove(offSet, attributesShowBox.Lines[myLine].Length + 1);
                attributesShowBox.Text = str1.ToString();
                temporaryAttributeList.RemoveAt(myLine);
                Invalidate();
                hasSelectedItem = false;//复位
            }
        }
        //************************************************************************************
        //鼠标按下响应
        [DllImport("user32.dll ")]//要搞清楚其原理
        public static extern System.Int32 HideCaret(System.IntPtr hwnd);
        private void attributesShowBox_MouseDown(object sender, MouseEventArgs e)
        {
            //选中Item
            if (attributesShowBox.Text.Length != 0)
            {
                int myStart = this.attributesShowBox.SelectionStart;//鼠标点击位置
                myLine = this.attributesShowBox.GetLineFromCharIndex(myStart);//获取选中行号(从0开始)
                if (attributesShowBox.Lines[myLine].Length > 0)
                {
                    //计算选中的行的开始位置
                    offSet = 0;
                    for (int i = 0; i < myLine; i++)
                    {
                        offSet += attributesShowBox.Lines[i].Length;
                        offSet += 1;//"\n"
                    }
                    //选中行
                    attributesShowBox.Select(offSet, attributesShowBox.Lines[myLine].Length);
                    hasSelectedItem = true;//置位
                }
                else
                    hasSelectedItem = false;
            }
            else
                hasSelectedItem = false;
            HideCaret(((RichTextBox)sender).Handle);//隐藏光标
            
        }

        private void button5_Click(object sender, EventArgs e)
        {

        }
        //************************************************************************************
        
    }
}
